package com.example.campuslink.commen.adpter

import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.core.CoreAdapter
import com.example.campuslink.commen.core.CoreViewHolder
import com.example.campuslink.commen.interfaces.CommendPublic
import com.example.campuslink.commen.tools.Sp
import com.example.campuslink.commen.tools.SpKey
import com.example.campuslink.databinding.AdapterInvitationChildrenCommendBinding
import com.example.campuslink.model.CommendInfoData
import com.example.campuslink.viewModel.InvitationDetailViewModel
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class InvitationChildrenCommendAdapter(
    vm: InvitationDetailViewModel,
    val commendPublic: CommendPublic,
) : CoreAdapter<AdapterInvitationChildrenCommendBinding, InvitationDetailViewModel, CommendInfoData>(
    BR.data,
    BR.viewModel,
    vm
) {
    private fun transformTime(time: Long): String {
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
        val formattedTime = sdf.format(Date(time))
        return formattedTime
    }

    override fun getItemLayoutId(viewType: Int): Int {
        return R.layout.adapter_invitation_children_commend
    }

    override fun onBindViewHolder(holder: CoreViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        Glide.with(holder.itemView.context).load(mListData[position].userImg)
            .error(R.drawable.ic_empty)
            .apply(RequestOptions.circleCropTransform())
            .into(holder.binding.root.findViewById<ImageView>(R.id.iv_userImg))
        holder.binding.root.findViewById<TextView>(R.id.tv_userName).text =
            mListData[position].userName
        holder.binding.root.findViewById<TextView>(R.id.tv_content).text =
            mListData[position].commendTable?.commentContent + "\t" + transformTime(
                mListData[position].commendTable?.data ?: 0
            )
        Glide.with(holder.itemView.context).load(mListData[position].commendTable?.contentImage)
            .apply(RequestOptions.circleCropTransform())
            .into(holder.binding.root.findViewById<ImageView>(R.id.iv_contentImg))
        holder.binding.root.findViewById<TextView>(R.id.tv_like_count).text =
            mListData[position].commendTable?.loveNumber?.size.toString()

        holder.binding.root.findViewById<TextView>(R.id.tv_reCommend).setOnClickListener({
            commendPublic.publicCommend(
                mListData[position].parentPosition,
                mListData[position].commendTable?.commentUserNumber ?: ""
            )
        })
        holder.binding.root.setOnClickListener({
            commendPublic.publicCommend(
                mListData[position].parentPosition,
                mListData[position].commendTable?.commentUserNumber ?: ""
            )
        })
        if (mListData[position].commendTable?.loveNumber?.contains(
                Sp.getString(
                    holder.itemView.context,
                    SpKey.USER_NUMBER.key,
                    ""
                )
            ) ?: false
        ) {
            Glide.with(holder.itemView.context).load(R.drawable.ic_collected)
                .into(holder.binding.root.findViewById<ImageView>(R.id.iv_like))
        } else {
            Glide.with(holder.itemView.context).load(R.drawable.ic_collect)
                .into(holder.binding.root.findViewById<ImageView>(R.id.iv_like))
        }
        holder.binding.root.findViewById<ImageView>(R.id.iv_like).setOnClickListener({
            if (mListData[position].commendTable?.loveNumber?.contains(
                    Sp.getString(
                        holder.itemView.context,
                        SpKey.USER_NUMBER.key,
                        ""
                    )
                ) ?: false
            ) {
                mViewModel?.removeLikeCommend(
                    false, mListData[position].commendTable?.id ?: 0,
                    mListData[position].commendTable?.commentUserNumber ?: "",
                    mListData[position].parentPosition
                )
            } else {
                mViewModel?.addLikeCommend(
                    false,
                    mListData[position].commendTable?.id ?: 0,
                    mListData[position].commendTable?.commentUserNumber ?: "",
                    mListData[position].parentPosition
                )
            }

            notifyDataSetChanged()
        })
        holder.binding.root.findViewById<TextView>(R.id.tv_like_count).text =
            mListData[position].commendTable?.loveNumber?.size.toString()
    }
}